/* ========================================================================
 * Copyright (c) 2005-2016 The OPC Foundation, Inc. All rights reserved.
 *
 * OPC Foundation MIT License 1.00
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * The complete license agreement can be found here:
 * http://opcfoundation.org/License/MIT/1.00/
 * ======================================================================*/

namespace Opc.Ua.Design.Schema {
    using System;

    /// <summary>
    /// Instance design extensions
    /// </summary>
    public static class InstanceDesignEx {

        /// <summary>
        /// Merge node design into the instance
        /// </summary>
        /// <param name="mergedInstance"></param>
        /// <param name="source"></param>
        public static void MergeIn(this InstanceDesign mergedInstance, NodeDesign source) {
            if (source.DisplayName != null && !source.DisplayName.IsAutogenerated) {
                mergedInstance.DisplayName = source.DisplayName;
            }
            if (source.Description != null && !source.Description.IsAutogenerated) {
                mergedInstance.Description = source.Description;
            }

            if (source is InstanceDesign instance) {
                if (mergedInstance.SymbolicName != source.SymbolicName) {
                    mergedInstance.SymbolicName = source.SymbolicName;
                    mergedInstance.BrowseName = source.BrowseName;
                    mergedInstance.DisplayName = source.DisplayName;
                }
                mergedInstance.ReferenceType = instance.ReferenceType;
                if (instance.ModellingRuleSpecified) {
                    mergedInstance.ModellingRule = instance.ModellingRule;
                    mergedInstance.ModellingRuleSpecified = true;
                }
                // Merge in instance design
                switch (instance) {
                    case VariableDesign variable:
                        mergedInstance.MergeIn(variable);
                        break;
                    case ObjectDesign objectd:
                        mergedInstance.MergeIn(objectd);
                        break;
                    case MethodDesign method:
                        mergedInstance.MergeIn(method);
                        break;
                }
            }
            else {
                // Merge in type design
                switch (source) {
                    case VariableTypeDesign variableType:
                        mergedInstance.MergeIn(variableType);
                        break;
                    case ObjectTypeDesign objectType:
                        mergedInstance.MergeIn(objectType);
                        break;
                }
            }
        }

        /// <summary>
        /// Merge in variable type design
        /// </summary>
        /// <param name="mergedInstance"></param>
        /// <param name="variableType"></param>
        private static void MergeIn(this InstanceDesign mergedInstance, VariableTypeDesign variableType) {
            if (!(mergedInstance is VariableDesign mergedVariable)) {
                throw new ArgumentException(nameof(mergedInstance));
            }
            mergedVariable.TypeDefinition = variableType.SymbolicId;
            mergedVariable.TypeDefinitionNode = variableType;

            if (variableType.DecodedValue != null) {
                mergedVariable.DecodedValue = variableType.DecodedValue;
            }
            if (variableType.DataType != null &&
                variableType.DataType != Constants.BaseDataType) {
                mergedVariable.DataType = variableType.DataType;
                mergedVariable.DataTypeNode = variableType.DataTypeNode;
            }
            if (variableType.ValueRankSpecified) {
                mergedVariable.ValueRank = variableType.ValueRank;
                mergedVariable.ValueRankSpecified = true;
            }
            if (!string.IsNullOrEmpty(variableType.ArrayDimensions)) {
                mergedVariable.ArrayDimensions = variableType.ArrayDimensions;
            }
            if (variableType.AccessLevelSpecified) {
                mergedVariable.AccessLevel = variableType.AccessLevel;
                mergedVariable.AccessLevelSpecified = true;
            }
            if (variableType.MinimumSamplingIntervalSpecified) {
                mergedVariable.MinimumSamplingInterval = variableType.MinimumSamplingInterval;
                mergedVariable.MinimumSamplingIntervalSpecified = true;
            }
            if (variableType.HistorizingSpecified) {
                mergedVariable.Historizing = variableType.Historizing;
                mergedVariable.HistorizingSpecified = true;
            }
        }

        /// <summary>
        /// Merge with type design
        /// </summary>
        /// <param name="mergedInstance"></param>
        /// <param name="objectType"></param>
        private static void MergeIn(this InstanceDesign mergedInstance, ObjectTypeDesign objectType) {
            if (!(mergedInstance is ObjectDesign mergedObject)) {
                throw new ArgumentException(nameof(mergedInstance));
            }
            mergedObject.TypeDefinition = objectType.SymbolicId;
            mergedObject.TypeDefinitionNode = objectType;

            if (objectType.SupportsEventsSpecified) {
                mergedObject.SupportsEvents = objectType.SupportsEvents;
                mergedObject.SupportsEventsSpecified = true;
            }
        }

        /// <summary>
        /// Merge in variable design
        /// </summary>
        /// <param name="mergedInstance"></param>
        /// <param name="variable"></param>
        private static void MergeIn(this InstanceDesign mergedInstance, VariableDesign variable) {
            if (!(mergedInstance is VariableDesign mergedVariable)) {
                throw new ArgumentException(nameof(mergedInstance));
            }
            if (variable.TypeDefinition != null &&
                variable.TypeDefinition != Constants.BaseDataVariableType) {
                mergedVariable.TypeDefinition = variable.TypeDefinition;
                mergedVariable.TypeDefinitionNode = variable.TypeDefinitionNode;
            }
            if (variable.DecodedValue != null) {
                mergedVariable.DecodedValue = variable.DecodedValue;
            }
            if (variable.DataType != null &&
                variable.DataType != Constants.BaseDataType) {
                mergedVariable.DataType = variable.DataType;
                mergedVariable.DataTypeNode = variable.DataTypeNode;
            }
            if (variable.ValueRankSpecified) {
                mergedVariable.ValueRank = variable.ValueRank;
                mergedVariable.ValueRankSpecified = true;
            }
            if (!string.IsNullOrEmpty(variable.ArrayDimensions)) {
                mergedVariable.ArrayDimensions = variable.ArrayDimensions;
            }
            if (variable.AccessLevelSpecified) {
                mergedVariable.AccessLevel = variable.AccessLevel;
                mergedVariable.AccessLevelSpecified = true;
            }
            if (variable.MinimumSamplingIntervalSpecified) {
                mergedVariable.MinimumSamplingInterval = variable.MinimumSamplingInterval;
                mergedVariable.MinimumSamplingIntervalSpecified = true;
            }
            if (variable.HistorizingSpecified) {
                mergedVariable.Historizing = variable.Historizing;
                mergedVariable.HistorizingSpecified = true;
            }
        }

        /// <summary>
        /// Merge with method design
        /// </summary>
        /// <param name="mergedInstance"></param>
        /// <param name="method"></param>
        private static void MergeIn(this InstanceDesign mergedInstance, MethodDesign method) {
            if (!(mergedInstance is MethodDesign mergedMethod)) {
                throw new ArgumentException(nameof(mergedInstance));
            }
            if (method.NonExecutableSpecified) {
                mergedMethod.NonExecutable = method.NonExecutable;
                mergedMethod.NonExecutableSpecified = true;
            }
        }

        /// <summary>
        /// Merge in object design
        /// </summary>
        /// <param name="mergedInstance"></param>
        /// <param name="objectDesign"></param>
        private static void MergeIn(this InstanceDesign mergedInstance, ObjectDesign objectDesign) {
            if (!(mergedInstance is ObjectDesign mergedObject)) {
                throw new ArgumentException(nameof(mergedInstance));
            }
            if (objectDesign.TypeDefinition != null &&
                objectDesign.TypeDefinition != Constants.BaseObjectType) {
                mergedObject.TypeDefinition = objectDesign.TypeDefinition;
                mergedObject.TypeDefinitionNode = objectDesign.TypeDefinitionNode;
            }

            if (objectDesign.SupportsEventsSpecified) {
                mergedObject.SupportsEvents = objectDesign.SupportsEvents;
                mergedObject.SupportsEventsSpecified = true;
            }
        }

    }
}

